package com.sl.utils;

import java.net.URL;
import java.util.Map;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*import org.apache.commons.lang.StringUtils;*/

/**
 * @author chuck1
 *
 */
public class BaiduAPI {
	// 百度到的ak
	// private static String ak = "5ef2641d89438a6e708db122820cf1d2";
	// 我申请的ak
	private static String ak = "LoVYj9ZcpYoXyp4u77Gvz5nbSis3c7Fi";

	public static Map<String, String> testPost(String x, String y) throws IOException {
		URL url = new URL("http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&callback=renderReverse&location=" + x
				+ "," + y + "&output=json&pois=1");
		URLConnection connection = url.openConnection();
		/**
		 * 然后把连接设为输出模式。URLConnection通常作为输入来使用，比如下载一个Web页。
		 * 通过把URLConnection设为输出，你可以把数据向你个Web页传送。下面是如何做：
		 */
		connection.setDoOutput(true);
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
		// remember to clean up
		out.flush();
		out.close();
		// 一旦发送成功，用以下方法就可以得到服务器的回应：
		String res;
		InputStream l_urlStream;
		l_urlStream = connection.getInputStream();
		BufferedReader in = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
		StringBuilder sb = new StringBuilder("");
		while ((res = in.readLine()) != null) {
			sb.append(res.trim());
		}
		String str = sb.toString();
		Map<String, String> map = null;
		if (StringUtils.hasText(str)) {
			int addStart = str.indexOf("formatted_address\":");
			int addEnd = str.indexOf("\",\"business");
			if (addStart > 0 && addEnd > 0) {
				String address = str.substring(addStart + 20, addEnd);
				map = new HashMap<String, String>();
				map.put("address", address);
				return map;
			}
		}

		return null;

	}

	
	
	
//	public static void main(String[] args) throws IOException {
//		double lat=106.568248;
//		double lng=29.495645;
//		double[] res=GPSToBaidu.wgs2bd(lat, lng);
//		System.out.println(res[0]+","+res[1]);
////		String coords = "106.568248,29.495645";
//		Map<String, String> json = BaiduAPI.testPost(String.valueOf(res[1]), String.valueOf(res[0]));
//		System.out.println(json);
//		System.out.println("address :" + json.get("address"));
//	}
}